package cn.xm.lingshi.utlis;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;



import cn.xm.lingshi.controller.PropertiesConfig;
import cn.xm.lingshi.pojo.AlipayBean;

public class AlipayUtil {
	public static String serverUrl = PropertiesConfig.getValue("url");// 支付宝网关
	public static String appId = PropertiesConfig.getValue("appID");// appId
	public static String privateKey = PropertiesConfig.getValue("rsaPrivateKey");// 商户私钥
	public static String charSet = PropertiesConfig.getValue("charset");// 编码
	public static String format = PropertiesConfig.getValue("format");// 返回格式
	public static String signType = PropertiesConfig.getValue("signType");// 签名方式
	public static String alipayPublicKey = PropertiesConfig
			.getValue("alipayPublicKey");// 支付宝公钥
	public static String notifyUrl = PropertiesConfig.getValue("notifyUrl");// 服务器异步通知页面路径
	public static String returnUrl = PropertiesConfig.getValue("returnUrl");// 页面跳转同步通知页面路径

	public static String connect(AlipayBean bean) throws AlipayApiException {
		//1.获取初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId,
				privateKey, format, charSet, alipayPublicKey, signType);
		//2.设置请求参数
		AlipayTradePagePayRequest alipayRequest=new AlipayTradePagePayRequest();
		//页面跳转同步通知页面路径
		alipayRequest.setReturnUrl(returnUrl);
		//服务器异步通知页面路径
		alipayRequest.setNotifyUrl(notifyUrl);
		//封装参数
		alipayRequest.setBizContent(JSON.toJSONString(bean));
		
		//3.请求支付宝进行付款，并获取支付结果
		return alipayClient.pageExecute(alipayRequest).getBody();

	}
}
